<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Designer extends MY_Luckydog {
    public function __construct()
    {

        parent::__construct();
        $this->load->model('luckydog/Designer_model', 'designer');
        
    }
	public function index()
	{
        $_SESSION['v_menu'] = 'designer';
        $designerlist = $this->designer->get_designer_list();
        foreach ($designerlist as $key => $value) {
             $designerlist[$key]['imageurl'] = $this->designer->get_img_list_byid($value['id']);
        }
        
        $data=array(
            'designerlist'=>$designerlist,
        );
		$this->load->view('luckydog/designer/designer_list',$data);
	}
     //添加分类 html
    public function add()
    {

        $this->load->view('luckydog/designer/designerext',$data);
    }
    //修改分类 html
    public function edit()
    {
        $designerid  = $this->input->get("id");
        $designerdetail = $this->designer->get_designer_by_id($designerid);
        $designer_img_list = $this->designer->get_img_list_byid($designerid);
        $data=array(
            'designerdetail'=>$designerdetail,
            'designer_img'=>$designer_img_list,
        );
        $this->load->view('luckydog/designer/designerext',$data);
    }
   
    public function do_add(){
        $categoryid   = $this->input->post("categoryid");
        $name   = $this->input->post("name");
        $original_arr = $this->input->post("original_arr");
        $default_arr = $this->input->post("default_arr");

        $data = array(
            "name"=>$name,
            "addtime"=>time(),
        );
       $insert_id = $this->designer->add_designer($data);
        //添加图书背景图片
        $status = 0;
        if($insert_id && !empty($original_arr)){
            foreach($default_arr as $def){
                if($def == 1){
                    $status = 1;
                }
            }
            if($status != 1){
                $default_arr[0] = 1;
            };
            foreach ($original_arr as $key => $value) {
                $temp = array(
                    "itemid"=>$insert_id,
                    "imgurl"=>$value,
                    "is_default"=>$default_arr[$key]
                    );
                $this->designer->add_item_img($temp);
            }
        }
        if($insert_id){
            echo json_encode(array('status'=>1,'message'=>'添加成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'添加失败'));
        }
        exit;
    }
    public function do_edit(){
        $id   = $this->input->post("id");
        $name   = $this->input->post("name");
        $original_arr = $this->input->post("original_arr");
        $default_arr = $this->input->post("default_arr");
        $ids_arr = $this->input->post("ids_arr");

        $data = array(
            "name"=>$name,
        );

        if (empty($id)){
            echo json_encode(array('status'=>-1,'message'=>'参数错误！'));
            exit;
        }
        $edit_flag = $this->designer->edit_designer($id,$data);
         //添加图书背景图片
        $status = 0;
        if(!empty($original_arr)){
            foreach($default_arr as $def){
                if($def == 1){
                    $status = 1;
                }
            }
            if($status != 1){
                $default_arr[0] = 1;
            };
            foreach ($original_arr as $key => $value) {
                if($ids_arr[$key]){
                    //执行修改
                    $temp = array(
                        "is_default"=>$default_arr[$key]
                    );
                    $res = $this->designer->edit_designer_img($ids_arr[$key],$temp);
                }else{
                    //执行添加
                    $temp = array(
                        "itemid"=>$itemid,
                        "imgurl"=>$value,
                        "is_default"=>$default_arr[$key]
                    );
                    $this->designer->add_item_img($temp);
                }
            }
        }

        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'商品修改成功'));
        }else{
            echo json_encode(array('status'=>-2,'message'=>'商品修改失败'));
        }
        exit;
    }
    public function do_del(){
        $id  = $this->input->post("id");
        $this->designer->del_designer($id);
        echo json_encode(array('status'=>1));
        exit;
    }

    public function upload_cover()
    {
        $this->load->library('Upload');
        $upload = new Upload();
        $file_name =$upload->execute($_FILES['Filedata']);
       
        if(is_string($file_name))
        {
            $res=json_encode(array('code'=>1,'type'=>0,'file_name'=>$file_name));

            echo $res;
        }
        else
        {
            echo json_encode(array('code'=>-1));
        }
    }

    /**
     * 删除图片
     */
     public function del_pic()
    {

        $this->load->library('upload');
        $imgsource = $this->input->post('imgsource', TRUE) ? $this->input->post('imgsource', TRUE) : '';

        //删除图片文件
        if (!empty($imgsource)) {
            $upload = new Upload();
            $res = $upload->del_file($imgsource);
        }
        //删除数据库数据
        $id = $this->input->post('id', TRUE) ? $this->input->post('id', TRUE):0;
        if($id){
            $res = $this->designer->del_attach($id);    
        }
        
        echo $res;

    }

}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */